﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace DnDDB
{
    public class Skill
    {
        int id;
        int ranks;
        string name;
        string charactername;
        public Skill(string name)
        {
            this.name = name;
            this.ranks = 0;
            this.findid();
        }
        public Skill(string name, int ranks)
        {
            this.name = name;
            this.ranks = ranks;
            this.findid();
        }
        public Skill(int id)
        {
            this.id = id;
            this.findname();
            this.ranks = 0;
        }
        public Skill(int id, int ranks)
        {
            this.id = id;
            this.findname();
            this.ranks = ranks;
        }

        private void findname()
        {
            string dbcommand = "Select name from skill where id='" + this.id + "';";
            getMySQL get = new getMySQL();
            DataRow dr = get.GetMySQLFilledRow(dbcommand);
            this.name = dr["name"].ToString();
        }

        private void findid()
        {
            string dbcommand = "Select id from skill where name='" + this.name + "';";
            getMySQL get = new getMySQL();
            DataRow dr = get.GetMySQLFilledRow(dbcommand);
            this.id = Convert.ToInt32(dr["id"]);
        }

        public void findranks()
        {
            if (charactername != null && charactername != "")
            {
                findranks(this.charactername);
            }
        }

        public void findranks(string charactername)
        {
            this.charactername = charactername;
            try
            {
                string dbcommand = "select ranks from character_knows_skill where skill_id='" + this.id + "' AND Character_name='" + charactername + "';";
                getMySQL get = new getMySQL();
                DataRow dr = get.GetMySQLFilledRow(dbcommand);
                ranks = Convert.ToInt32(dr["ranks"]);
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
                ranks = 0;
            }
        }

        public int getRanks()
        {
            return this.ranks;
        }

        public void setRanks(int ranks)
        {
            this.ranks = ranks;
        }

        public string getCharacterName()
        {
            return this.charactername;
        }

        public void setCharacterName(string charactername)
        {
            this.charactername = charactername;
        }

        public int getID()
        {
            return this.id;
        }

        public void setID(int id)
        {
            this.id = id;
        }

        public string getName(string name)
        {
            return this.name;
        }

        public void save()
        {
            if (charactername != null && charactername != "")
            {
                try
                {
                    string dbcommand = "select ranks from character_knows_skill where skill_id='" + this.id + "' AND Character_name='" + charactername + "';";
                    getMySQL get = new getMySQL();
                    DataTable dr = get.GetMySQLFilledTable(dbcommand);
                    if (dr.Rows.Count > 0)
                    {
                        dbcommand = "update character_knows_skill set ranks='" + this.ranks + "' where skill_id='" + this.id + "' AND Character_name='" + charactername + "';";

                    }
                    else
                    {
                        dbcommand = "insert into character_knows_skill values ('" + this.charactername + "', '" + this.id + "', '" + this.ranks + "');";
                    }
                    get.GetMySQLExecuteNonQuery(dbcommand);
                }
                catch
                {
                }
            }
            else
            {
                MessageBox.Show("Could not save the skill. You're either missing the id or the character name.");
            }
        }
    }
}
